﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NetSurveillance.WebApp.DataServices.Models;
using NetSurveillance.WebApp.Models;

namespace NetSurveillance.WebApp.Controllers
{
    public class CaseController : BaseController
    {
        public ActionResult SaveCase(long? caseId = null)
        {
            CaseModel caseModel = null;
            if (caseId.HasValue)
            {
                var caseObj = DataProvider.CaseService.GetModelById(caseId.Value);
                if (caseObj != null)
                    caseModel = new CaseModel { Id = caseObj.Id, Name = caseObj.Name, Desc = caseObj.Desc };
            }
            return PartialView("_SaveCace", caseModel);
        }

        [HttpPost]
        public ActionResult SaveCase(CaseModel caseModel)
        {
            if (ModelState.IsValid)
            {
                long caseId = caseModel.Id.HasValue ? caseModel.Id.Value : -1;
                bool hasSameName = DataProvider.CaseService.GetAllModels(false,
                                                                        p =>
                                                                        p.Name == caseModel.Name &&
                                                                        (!caseModel.Id.HasValue || p.Id != caseId)).Any();
                if (hasSameName)
                    return Json(new AjaxResult(false, Resources.MessageResource.CaseIsExists));
                var caseEntry = caseModel.Id.HasValue
                                    ? DataProvider.CaseService.GetModelById(caseId)
                                    : new Case();
                if (caseModel.Id.HasValue)
                    caseEntry.LastModifiedTime = DateTime.Now;
                else
                    caseEntry.CreateUserId = CurrentUser.Id;
                caseEntry.Name = caseModel.Name;
                caseEntry.Desc = caseModel.Desc;
                DataProvider.CaseService.Save(caseEntry);
                var result = new AjaxResult(true)
                    {
                        Expandos = new {caseEntry.Name, caseEntry.Id}
                    };
                return Json(result);
            }
            return Json(new AjaxResult(false, Resources.MessageResource.Save_Error));
        }

        public ActionResult SaveKeywordTemplate(long entryId = 0)
        {
            KeywordTemplate keywordTemplate = DataProvider.KeywordService.GetKeywordTemplateById(entryId);

            return PartialView("_SaveKeywordTemplate", keywordTemplate ?? new KeywordTemplate());
        }

        [HttpPost]
        public ActionResult SaveKeywordTemplate(KeywordTemplate entry)
        {
            if (ModelState.IsValid)
            {
                DataProvider.KeywordService.SaveKeywordToTemplate(entry);
                return
                    Json(new AjaxResult(true)
                        {
                            Expandos =
                                new { entry.KeywordName, entry.Id }
                        });
            }
            return Json(new AjaxResult(false, Resources.MessageResource.Save_Error));
        }

        [HttpPost]
        public ActionResult DeleteKeywordTemplate(long entryId = 0)
        {
            var serviceResult = DataProvider.KeywordService.DeleteKeywordFromTemplate(entryId);
            if (serviceResult.ExecutedSuccess && serviceResult.EffectNumber > 0)
                return Json(new AjaxResult(true));
            return Json(new AjaxResult(false, Resources.MessageResource.Delete_Error));
        }

        [HttpPost]
        public ActionResult ClearCase(long caseId)
        {
            if (CurrentCase != null && CurrentCase.Id == caseId)
            {
                return CamelCaseJson(new AjaxResult(false, "当前案件中存在运行或暂停中的会话，无法清除该案件数据"));
            }
            var dataRecords = DataProvider.DataRecordService.GetDataRecords(caseId, " and \"FilePath\" is not null");
            //todo:清除案件下的文件
            var sr = DataProvider.DataRecordService.ClearDataRecords(caseId);
            return CamelCaseJson(new AjaxResult(sr.ExecutedSuccess, string.Format("清空的记录数为：{0}", sr.EffectNumber)));
        }
    }
}
